package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.BaseEntity;
import net.starwifi.duobao.entity.Operator;
import net.starwifi.duobao.entity.Role;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.service.OperatorService;
import net.starwifi.duobao.service.RoleService;
import net.starwifi.duobao.utils.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;

/**
 * Controller - OperatorController.
 *
 * @author ruoo
 * @version 1.0
 */
@Controller("adminOperatorController")
@RequestMapping("/operator")
public class OperatorController extends BaseController {

    @Autowired
    private OperatorService operatorService;

    @Autowired
    private RoleService roleService;




    /**
     * 检查用户名是否存在
     */
    @RequestMapping(value = "/check_username", method = RequestMethod.GET)
    public @ResponseBody
    boolean checkUsername(String username) {
        if (org.apache.commons.lang.StringUtils.isEmpty(username)) {
            return false;
        }
        if (operatorService.usernameExists(username)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        model.addAttribute("roles", roleService.getListData());
        return "/operator/add";
    }


    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Operator operator, Long[] roleIds, RedirectAttributes redirectAttributes) {
        operator.setRoles(new HashSet<Role>(roleService.findList(roleIds)));
        if (!isValid(operator, BaseEntity.Save.class)) {
            return ERROR_VIEW;
        }
        if (operatorService.usernameExists(operator.getUsername())) {
            return ERROR_VIEW;
        }
        String salt= StringUtils.getUUID();
        operator.setPassword(DigestUtils.md5Hex(operator.getPassword()+salt));
        operator.setSalt(salt);
        operator.setIsLocked(false);
        operator.setLoginFailureCount(0);
        operator.setLockedDate(null);
        operator.setLoginDate(null);
        operator.setLoginIp(null);

        operatorService.save(operator);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }


    /**
     * 保存
     */
    @RequestMapping(value = "/fqg")
    public String save( RedirectAttributes redirectAttributes) {
        Role role=roleService.find(3L);
        for(int i=1;i<=200;i++){
            Operator operator=new Operator();
            String salt= StringUtils.getUUID();
            String username="hb_fqg_"+getUsername(i);
            operator.setUsername(username);
            operator.setEmail(username+"@189.cn");
            operator.setIsEnabled(Boolean.TRUE);
            operator.setDepartment("营业厅");
            operator.getRoles().add(role);
            operator.setPassword(DigestUtils.md5Hex(StringUtils.toMD5(username).substring(0,10)+salt));
            operator.setSalt(salt);
            operator.setIsLocked(false);
            operator.setLoginFailureCount(0);
            operator.setLockedDate(null);
            operator.setLoginDate(null);
            operator.setLoginIp(null);
            operatorService.save(operator);
        }
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }


    public static String getUsername(int i){
        if(i<10){
            return  ("00"+i);
        }
        if(i<100){
            return  ("0"+i);
        }else return i+"";
    }


    public static void main(String[] args) {
        for(int i=1;i<=200;i++){
            System.out.println(getUsername(i));
        }
    }




    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("roles", roleService.getListData());
        model.addAttribute("entity", operatorService.find(id));
        return "/operator/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Operator operator, Long[] roleIds, RedirectAttributes redirectAttributes) {
        operator.setRoles(new HashSet<Role>(roleService.findList(roleIds)));
        if (!isValid(operator)) {
            return ERROR_VIEW;
        }
        Operator pOperator = operatorService.find(operator.getId());
        if (operator == null) {
            return ERROR_VIEW;
        }
        String salt=StringUtils.getUUID();
        if (org.apache.commons.lang.StringUtils.isNotEmpty(operator.getPassword())) {
            operator.setSalt(salt);
            operator.setPassword(DigestUtils.md5Hex(operator.getPassword()+salt));
        } else {
            operator.setSalt(pOperator.getSalt());
            operator.setPassword(pOperator.getPassword());
        }
        if (pOperator.getIsLocked() && !operator.getIsLocked()) {
            operator.setLoginFailureCount(0);
            operator.setLockedDate(null);
        } else {
            operator.setIsLocked(pOperator.getIsLocked());
            operator.setLoginFailureCount(pOperator.getLoginFailureCount());
            operator.setLockedDate(pOperator.getLockedDate());
        }
        operatorService.update(operator, "username", "loginDate", "loginIp", "orders");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageView pageView, ModelMap model,HttpServletRequest request) {
        model.addAttribute("page", operatorService.getScrollData(pageView));
        return "/operator/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Message delete(Long[] ids) {
        operatorService.delete(ids);
        return SUCCESS_MESSAGE;
    }






}
